package com.letv.core.download.image;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.text.TextUtils;

import com.letv.core.utils.FileUtils;
import com.letv.core.utils.MD5;

/**
 * 图片磁盘缓存管理器
 * 
 * @author zhuqiao
 * 
 */
public class ImgFileManager {
    private static String foldPath = "";

    /**
     * 获取图片缓存文件夹路径
     * 
     * @return
     */
    private static String getFolder(Context context) {
        if (TextUtils.isEmpty(foldPath)) {
            foldPath = FileUtils.getBitmapCachePath(context);
            if (!foldPath.endsWith("/")) {
                foldPath = foldPath + "/";
            }
        }
        return foldPath;
    }

    /**
     * 保存图片
     * 
     * @param url
     * @param data
     */
    public static void saveImage(Context context, String url, byte[] data) {
        String path = getFolder(context);
        File folder = new File(path);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        File file = new File(path + MD5.MD5Encode(url) + ".img");
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(data);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bos != null) {
                    bos.flush();
                    bos.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取图片
     * 
     * @param url
     * @return
     */
    public static byte[] getImage(Context context, String url) {
        String path = getFolder(context);
        path += MD5.MD5Encode(url) + ".img";

        byte[] buffer = null;
        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;
        try {
            File file = new File(path);
            if (!file.exists()) {
                return null;
            }
            fis = new FileInputStream(file);
            bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            buffer = bos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bos != null)
                    bos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                if (fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return buffer;
    }

    /**
     * 获取图片在SD卡中的路径
     * 
     * @param name
     * @return
     */
    public static String getBitmapPath(Context context, String name) {
        String path = getFolder(context);
        File folder = new File(path);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        return path + MD5.MD5Encode(name) + ".img";
    }

}
